// r202

package dataHardware;

import hardwareToSQL.SensorData;
import hardwareToSQL.Separator;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.util.AbstractQueue;
import java.util.Hashtable;
import java.util.LinkedList;
import java.util.Properties;
import java.util.Queue;
import java.util.Stack;

public class Modem implements Runnable{
	private Properties connectionConfig;
	private Socket skt;
	private PrintWriter out;
	private ByteBuffer bbuf;
	private LinkedList<Byte> bQueue;
	
	public Modem(){
		init();
	}
	private void init() {

		//Load the properties
		try {
			
			connectionConfig = new Properties();
			connectionConfig.load(new FileInputStream
					("properties/connection_config.properties"));
		} catch (FileNotFoundException e) {
			System.err.print("\nFailed to load property file\n");
			System.exit(1);
		} catch (IOException e) {
			System.err.print("\nFailed to load property file\n");
			System.exit(1);
		} catch (NullPointerException e) {
			System.err.print("\nFailed to load property file\n");
			System.exit(1);
		}
		
		bQueue = new LinkedList<Byte>();
		
		//Set default value to sensor data object so that it can
		//be given when no data is yet available
		//Initialize the buffers for storing the names and values of input
	}
	private void makeConnection() throws IOException {
		//Connect server to the port
		try {

			int port = Integer.parseInt(
				connectionConfig.getProperty("Server_port"));
			InetAddress ip = InetAddress.getByName(
				connectionConfig.getProperty("Server_Address"));

			System.out.println(
				"\nWaiting to establish server connection at "
				+ ip + ":" + port);
			
			skt = new Socket(ip, port);

			System.out.println("\nEstablished connection to server");
		} 
		catch (IOException e) {
			System.err.print("\nFailed to server\n");
			System.exit(1);
		}
		

		//Establish the input from the client
		try {
			out = new PrintWriter( new OutputStreamWriter(
					skt.getOutputStream()));
		} catch (NullPointerException e) {
			System.err.print("\nFailed to create input reader for client\n");
			System.exit(1);
		} catch (IOException e) {
			System.err.print("\nFailed to create input reader for client\n");
			System.exit(1);
		} 
	}
	public void acceptBytes(byte[] bytes){
		for(byte b:bytes){
			bQueue.add(b);
		}
		
	}
	/**
	 * @param args
	 */
	public void sendByte(){
		
	}
	public static void main(String[] args) {
		
	}
	@Override
	public void run() {
		try {
			makeConnection();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		while (true){
			Byte b = bQueue.poll();
			if (b!=null){
				out.write(b);
			}
		}
		
	}

}
